﻿@using WSS.Models.Test
@{
    Layout = null;
}
@if (ViewBag.QList != null)
{
    int j = 0;
    IDictionary<List<TEST_QUESTION>, IDictionary<TEST_QUESTION, List<TEST_ANSWER>>> qList = ViewBag.QList;
    TEST_QUESTION qModel = new TEST_QUESTION();
    IDictionary<TEST_QUESTION, List<TEST_ANSWER>> qDetail = new Dictionary<TEST_QUESTION, List<TEST_ANSWER>>();
    foreach (var list in qList)
    {
        qDetail = list.Value;
        foreach (var oneModel in qDetail)
        {
            j++;
            qModel = oneModel.Key;
    <div class="con_stAll" id="@("q_" + j)" @if (j == list.Key.Count())
                                            {
                                                        @MvcHtmlString.Create("style=\"border-bottom: none\"")
                                            }>
        <div class="con_stAll_tg">
            <p style="line-height: 26px; float: left">
                <b>@j、</b>@qModel.QUESTION_TITLE
            </p>
            <div style="float: right;">
                <button type="button" class="btn btn-default btn-sm"  onclick="updateQuestion('@qModel.QUESTION_ID',@j) ">修改试题</button>
                <button type="button" class="btn btn-danger btn-sm"  onclick="delQuestion('@qModel.QUESTION_ID', @j) ">删除试题</button>
            </div>
            <div style="clear: both"></div>
            @foreach (var aModel in oneModel.Value)
            {
                <div class="checkbox">
                    <label> 
                        <input type="checkbox"  @if (aModel.FLAG_ISRIGHT == 1)
                                                {
                                                    @MvcHtmlString.Create("checked='true'")
                                                }   aId="@aModel.ANSWER_ID"/>
                      <span> @aModel.ANSWER_NO. @aModel.CONTENTS</span>
                    </label>
                </div>
                  
            }
        </div>
        <div class="con_stAll_da">
            <span style="float: left; margin-right: 10px; line-height: 25px;">题型设置：</span>
            <label val="0" onclick="setQType(this,'@qModel.QUESTION_ID','0')" @if (qModel.QUESTION_TYPE == 0)
                                                                      {
                                                                                @MvcHtmlString.Create("class='label_bj'")
                                                                      }>
                单选</label>
            <label val="1" onclick="setQType(this,'@qModel.QUESTION_ID','1')"  @if (qModel.QUESTION_TYPE == 1)
                                                                       {
                                                                                @MvcHtmlString.Create("class='label_bj'")
                                                                       }>
                多选</label>
            <label val="2" onclick="setQType(this,'@qModel.QUESTION_ID','2')" @if (qModel.QUESTION_TYPE == 2)
                                                                      {
                                                                                @MvcHtmlString.Create("class='label_bj'")
                                                                      }>
                判断</label>
        </div>
    </div>
        }
    }
    if (j == 0)
    {
    <div style="text-align: center;" id="EmptyAnswer_Div">
        <span style="font-size: 14px; vertical-align: middle;">无试题</span>
        <button type="button" class="btn btn-default" style="width: 80px; margin-left: 30px; height: 35px; margin-top: 5px;" onclick="addQuestion()">立即添加</button>
    </div>
    }

}
<script type="text/javascript">
    $(function() {
        $("input:checkbox").change(function () {
            var qType = $(this).parents(".con_stAll").find(".con_stAll_da .label_bj").attr("val");
            var aId = $(this).attr("aId");
            var curCheckState = $(this).is(":checked");
            var checkedCount = 0;
            if (curCheckState) {
                checkedCount = $(this).parents(".con_stAll_tg").find("input:checkbox:checked").size();//当前试题正确答案项个数
                if (checkedCount > 1) {
                    //单选和判断仅选取一个正确答案
                    if (parseInt(qType) != 1) {
                        mainfn.msg("当前题型只能设置一个正确答案项！如需选取多个，请设置题型为多选！");
                        $(this).attr("checked", false);
                    } else {
                        //多选题答案 修改到数据库
                        setRightAnswer(aId,1);
                    }
                } else {
                    //修改到数据库
                    setRightAnswer(aId,1);
                }
            } else {
                //修改到数据库(更改正确答案项)
                setRightAnswer(aId,0);
            }
        });
    });
    function updateQuestion(qId,qIndex) {
        var url = "/testCenter/TestQForm?id=" + qId + "&testId=" + $("#TestID").val();
        mainfn.window(url, {
            width: 800,
            height: 550,
            modal: true,
            title: "试题维护",
            level: mainfn.level.lvl_primary,
            onclose: function () {
                document.cookie = "curPosition=" + qIndex;
                location.reload();
            }
        });
    }

    function delQuestion(testId, divIndex) {
        var url = "/testCenter/DelTestQ";
        $.post(url, { id: testId }, function(msg) {
            if (msg.flag) {
                $("#q_" + divIndex).remove();
                var cateQCount = $(".list-inline a.active").find("strong").text();
                cateQCount = cateQCount.substring(1, cateQCount.indexOf(')'));
                $(".list-inline a.active").find("strong").text("(" + (parseInt(cateQCount) - 1) + ")");
            } else {
                mainfn.msg(msg.msg);
            }
        });
    }

    function addQuestion() {
        var url = "/testCenter/TestQForm?testId=" + $("#TestID").val() + "&cateNo=" + $(".list-inline a.active").attr("cateNo");
        mainfn.window(url, {
            width: 800,
            height: 550,
            modal: true,
            title: "试题维护",
            level: mainfn.level.lvl_primary,
            onclose: function () {
                location.reload();
            }
        });
    }

    function setQType(obj, qId, typeVal) {
        $.ajax({
            url: "/testCenter/SetQuestionType",
            data: { id: qId, type: typeVal },
            type: 'POST',
            success: function(data) {
                if (data.flag) {
                    $(obj).addClass("label_bj").siblings().removeClass("label_bj");
                }
                mainfn.msg(data.msg);
            }
        });
    }

    function setRightAnswer(aId, isRight) {
        $.ajax({
            url: "/testCenter/SetRightAnswer",
            data: { id: aId, isRight: isRight },
            type: 'POST',
            success: function(data) {
                mainfn.msg(data.msg);
            }
        });
    }
</script>
